//
//  NWNetworkTools.swift
//  Weibo
//
//  Created by Northwan on 16/1/22.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

enum NWHttpMethod :String{
    case GET = "GET"
    case POST = "POST"
}

class NWNetworkTools:AFHTTPSessionManager {
    
    // 给闭包类型定义一个别名
    typealias NWRequestCallBack = (response: AnyObject?,error: NSError?) ->Void
    
    //单例
    static let shareTools:NWNetworkTools = {
        let tools = NWNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    /**
     请求网络
     
     - parameter method:   请求方式
     - parameter url:      请求地址
     - parameter params:   参数
     - parameter callback: 请求回调
     */
     func request(method:NWHttpMethod,url:String,params:AnyObject?,finished:NWRequestCallBack){
        let succCallback = {(dataTask:NSURLSessionDataTask,response: AnyObject?)-> Void in
            
            guard let dict = response as? [String: AnyObject] else {
                // 如果不是字典，返回错误
                let error = NSError(domain: "com.itheima.error", code: -1001, userInfo: ["message": "The response data type isn't a [String: AnyObject]"])
                finished(response: nil, error: error)
                return
            }

            finished(response:dict,error:nil)
        }
        
        let failCallback = {(dataTask:NSURLSessionDataTask?,error: NSError)-> Void in
            finished(response:nil ,error:error)
        }
        
        if method == .GET {
            GET(url, parameters: params, success: succCallback, failure: failCallback)
        }else{
            POST(url, parameters: params, success: succCallback, failure: failCallback)
        }
    }
}

// MARK: - 首页微博数据
extension NWNetworkTools{
    func loadStatus(accessToken:String ,maxid:Int64 = 0,sinceid:Int64 = 0, finished:NWRequestCallBack){
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token":accessToken,
            "max_id":"\(maxid)",
            "since_id":"\(sinceid)"
        ]
        request(.GET, url: urlString, params: params,finished: finished)
    }
}

// MARK: - Oauth授权登录
extension NWNetworkTools{
    
    /**
     获取accessToken
     
     - parameter code:     授权码
     */
    func loadAccessToken(code:String,finished:NWRequestCallBack){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
            "client_id":kAppkey,
            "client_secret":kAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":kRedirectUri
        ]
        
        request(.POST, url: urlString, params: params,finished: finished)
    }
    
    /**
     获取用户信息
     
     - parameter userAccount: 用户信息model
     */
    func loadUserInfo(userAccount: NWUserAccount,finished:NWRequestCallBack){
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        request(.GET, url: urlString, params: params,finished: finished)
    }
}



